﻿Imports System.Environment

Public Class frmMenu
    Private szSQL As String
    Private rs As ADODB.Recordset = Nothing

    Private Sub frmMenu_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        ' lire les paramètres de l'application
        ctDSN_DEVFAC = My.Settings.ConnectionBD
        gSocieteCode = My.Settings.CodeSociete

        ' tester la connection avec la BD MySQL
        If IsConnectionOK() = False Then
            ' fin programme
            End
        End If

        ' Ouvrir le form Identification
        frmLogin.ShowDialog()
        If gUserCode = "___" Then
            ' fin programme
            End
        End If

        'gSocieteCode = "PE"
        'gUserNom = "TEST TEST"
        'gUserCode = "CAO"
        'gUserAcces = 9
        'gImpBlanc = "HP_SECRE"
        'gImpEntete = "HP_INFO"

        ' Lire la societe et affiche son nom
        szSQL = "CALL spGET_P_SOCIETE('" & gSocieteCode & "')"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)
        If rs.RecordCount > 0 Then
            lb_NomAgence.Text = rs.Fields("nom").Value
        Else
            lb_NomAgence.Text = "Non trouvé"
        End If
        rs.Close()
        rs = Nothing
        Me.Text = lb_NomAgence.Text & " - " & gUserNom
        Me.BackColor = getSocieteColor()
        ' afficher dans le menu les infos de l'utilisateur
        lb_UserNom.Text = gUserNom
        Me.TE_VERSION.Text = String.Format("Version {0}", My.Application.Info.Version.ToString)
        ' changer le logo
        Select Case gSocieteCode
            Case "ME"
                PB_LOGO.Image = My.Resources.logo_menuiserie
            Case "PE"
                PB_LOGO.Image = My.Resources.logo_peinture
            Case "PO"
                PB_LOGO.Image = My.Resources.logo_polynet
            Case "PL"
                PB_LOGO.Image = My.Resources.logo_versanit
        End Select

        ' initialiser le menu
        InitMenusTree()
        ' période = mois en cours
        gDate_debut = CDate(Today.Year.ToString & "/" & Today.Month.ToString & "/01")
        gDate_fin = Today

    End Sub

    ' Tester la connection avec la base de données
    Private Function IsConnectionOK() As Boolean

        Dim conn As ADODB.Connection

        'enable error handler
        On Error GoTo ErrorHandler

        IsConnectionOK = False
        conn = New ADODB.Connection

        ' tester la connection avec MySQL
        conn.Open(ctDSN_DEVFAC)
        conn.Close()
        ' conn.Dispose()
        IsConnectionOK = True
        Exit Function

ErrorHandler:
        MsgBox("Erreur lors de la connection à la BD : " & Err.Number & vbCrLf & Err.Description, MsgBoxStyle.Critical)
        End

    End Function

    Private Sub InitMenusTree()
        Dim nodeRub As TreeNode
        Dim noRub As Integer = 0
        Dim noSRub As Integer = 0

        TV_MENU.Nodes.Clear()
        TV_MENU.ImageList = Me.IM_MENU

        ' rubrique 0 : CLIENTS
        nodeRub = New TreeNode("CLIENTS")
        nodeRub.ImageIndex = 0
        TV_MENU.Nodes.Add(nodeRub)
        ' sous-rubrique : Fiche client
        nodeRub = New TreeNode("Fiche Client")
        nodeRub.ImageIndex = 1
        TV_MENU.Nodes(0).Nodes.Add(nodeRub)
        ' sous-rubrique : Satistiques client
        nodeRub = New TreeNode("Statistiques Client")
        nodeRub.ImageIndex = 1
        TV_MENU.Nodes(0).Nodes.Add(nodeRub)

        ' rubrique 1 : DOCUMENTS
        nodeRub = New TreeNode("CHANTIERS")
        nodeRub.ImageIndex = 0
        TV_MENU.Nodes.Add(nodeRub)
        ' sous-rubrique : Recherche chantier
        nodeRub = New TreeNode("Rechercher un chantier")
        nodeRub.ImageIndex = 4
        TV_MENU.Nodes(1).Nodes.Add(nodeRub)
        ' sous-rubrique : Planning des RDV
        nodeRub = New TreeNode("Planning des RDV")
        nodeRub.ImageIndex = 4
        TV_MENU.Nodes(1).Nodes.Add(nodeRub)
        ' sous-rubrique : Planning des Chantiers
        nodeRub = New TreeNode("Planning des chantiers")
        nodeRub.ImageIndex = 4
        TV_MENU.Nodes(1).Nodes.Add(nodeRub)
        ' sous-rubrique : RDV du jour
        nodeRub = New TreeNode("Rendez-vous du jour")
        nodeRub.ImageIndex = 4
        TV_MENU.Nodes(1).Nodes.Add(nodeRub)

        ' rubrique 2 : REGLEMENTS
        nodeRub = New TreeNode("POST-FACTURATION")
        nodeRub.ImageIndex = 0
        TV_MENU.Nodes.Add(nodeRub)
        ' sous-rubrique : Relance factures
        nodeRub = New TreeNode("Relance factures")
        nodeRub.ImageIndex = 5
        TV_MENU.Nodes(2).Nodes.Add(nodeRub)
        ' sous-rubrique : Relance OS
        nodeRub = New TreeNode("Relance OS")
        nodeRub.ImageIndex = 5
        TV_MENU.Nodes(2).Nodes.Add(nodeRub)
        ' sous-rubrique : Remise de chèques
        nodeRub = New TreeNode("Remise de chèques")
        nodeRub.ImageIndex = 2
        TV_MENU.Nodes(2).Nodes.Add(nodeRub)

        TV_MENU.ExpandAll()
        ' select la fiche élève
        TV_MENU.SelectedNode = TV_MENU.Nodes(0)

    End Sub



    Private Sub TV_MENU_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            TV_MENU_DoubleClick(sender, e)
        End If
    End Sub

    Private Sub QuitterToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuitterToolStripMenuItem.Click
        ' Fermer AEM2010
        Application.Exit()
    End Sub

    Private Sub CabinetsEtExperts_Click(sender As System.Object, e As System.EventArgs) Handles CabinetsEtExperts.Click
        frmCabinets.ShowDialog()
    End Sub

    Private Sub ArticlesEtTarifsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ArticlesEtTarifsToolStripMenuItem.Click
        frmArticles.ShowDialog()
    End Sub

    Private Sub TablesDeCodificationToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles TablesDeCodificationToolStripMenuItem.Click
        frmTablesCodes.ShowDialog()
    End Sub

    Private Sub AProposDeToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AProposDeToolStripMenuItem.Click
        frmApropos.ShowDialog()
    End Sub

    Private Sub TV_MENU_DoubleClick(sender As Object, e As System.EventArgs) Handles TV_MENU.DoubleClick
        Dim nodeRub As TreeNode = TV_MENU.SelectedNode

        ' Une feuille est sélectionnée ?
        If nodeRub.Level > 0 Then
            Select Case nodeRub.Parent.Text
                Case "CLIENTS"
                    ' menu clients
                    Select Case nodeRub.Index
                        Case 0
                            frmClients.ShowDialog()
                        Case 1
                            frmEditions.pRubrique = "STATS"
                            frmEditions.ShowDialog()

                    End Select

                Case "CHANTIERS"
                    ' menu documents
                    Select Case nodeRub.Index
                        Case 0
                            frmChantiers.ShowDialog()
                        Case 1
                            frmPlanning.pTypePlanning = "R"
                            frmPlanning.ShowDialog()
                        Case 2
                            frmPlanning.pTypePlanning = "C"
                            frmPlanning.ShowDialog()
                        Case 3
                            frmRendezvousJour.ShowDialog()

                    End Select

                Case "POST-FACTURATION"
                    ' menu post-facturation
                    Select Case nodeRub.Index
                        Case 0
                            frmRelanceFA.ShowDialog()
                        Case 1
                            frmRelanceOS.ShowDialog()
                        Case 2
                            frmRemiseCHQ.ShowDialog()
                    End Select
            End Select
        End If

    End Sub

    Private Sub ExportEnCompta_Click(sender As System.Object, e As System.EventArgs) Handles ExportEnCompta.Click
        frmExport.ShowDialog()
    End Sub

    Private Sub EditionStatistiques_Click(sender As System.Object, e As System.EventArgs) Handles EditionStatistiques.Click
        frmEditions.pRubrique = "JOURN"
        frmEditions.ShowDialog()
    End Sub

    Private Sub EditionDesJournaux_Click(sender As System.Object, e As System.EventArgs) Handles EditionDesJournaux.Click
        frmEditions.pRubrique = "STATS"
        frmEditions.ShowDialog()
    End Sub

    Private Sub ComptageDesDossiers_Click(sender As System.Object, e As System.EventArgs) Handles ComptageDesDossiers.Click
        frmComptage.ShowDialog()
    End Sub

    Private Sub TV_MENU_KeyDown1(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TV_MENU.KeyDown

        If e.KeyCode = Keys.Enter Then
            ' touche Enter = DoubleClick
            TV_MENU_DoubleClick(sender, e)
        End If

    End Sub

    Private Sub LettresTypeMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles LettresTypeMenuItem.Click
        frmLettresType.ShowDialog()
    End Sub

    Private Sub UtilisateursToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles UtilisateursToolStripMenuItem.Click
        frmUsers.ShowDialog()
    End Sub

    Private Sub MesPreferencesMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles MesPreferencesMenuItem.Click
        frmMesPreferences.ShowDialog()
    End Sub

    Private Sub MenuFicheSociete_Click(sender As System.Object, e As System.EventArgs) Handles MenuFicheSociete.Click
        frmSociete.ShowDialog()
    End Sub


End Class
